//
//  SLVisitorView.swift
//  SLBlog
//
//  Created by 盼 on 2017/12/6.
//  Copyright © 2017年 pan. All rights reserved.
//

import UIKit

class SLVisitorView: UIView {
    
    var visitorInfo: [String: String]? {
       
        didSet {
            guard let imageName = visitorInfo?["imageName"],
                let title = visitorInfo?["tipTitle"]
                else {
                return
            }
            
            //首页
            if imageName == "" {
                
                startAnimation()
                return
            }
            
            houseImage.isHidden = true
            maskImage.isHidden = true
            
            imageView.image = UIImage.init(named: imageName)
            titleLabel.text = title
        }
    }
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor().colorWithHex(rgb: 0xededed, alpha: 1.0)
        setVisitorView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var imageView:UIImageView = UIImageView(image: UIImage.init(named: "visitordiscover_feed_image_smallicon"))
    
    lazy var houseImage:UIImageView = UIImageView(image: UIImage.init(named: "visitordiscover_feed_image_house"))
    
    lazy var maskImage:UIImageView = UIImageView(image: UIImage.init(named: "visitordiscover_feed_mask_smallicon"))
    
    lazy var titleLabel:UILabel = {
        
        let label = UILabel()
        label.text = "快来登录看看，有什么惊喜不要错过！"
        label.font = UIFont.systemFont(ofSize: 16)
        label.textColor = UIColor.darkGray
        label.textAlignment = NSTextAlignment.center
        label.numberOfLines = 0
        
        return label;
    }()
        
    
    lazy var registerBtn: UIButton = {
       
        let btn = UIButton()
        btn.setTitle("注册", for: UIControlState.normal)
        btn.setTitleColor(UIColor.darkGray, for: UIControlState.normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        
        btn.setBackgroundImage(UIImage.init(named: "common_button_white_disable"), for: UIControlState.normal)
        
        return btn
    }()
    
    lazy var loginBtn: UIButton = {
        
        let btn = UIButton()
        btn.setTitle("登录", for: UIControlState.normal)
        btn.setTitleColor(UIColor.orange, for: UIControlState.normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        
        btn.setBackgroundImage(UIImage.init(named: "common_button_white_disable"), for: UIControlState.normal)
        return btn
        
    }()
}


extension SLVisitorView
{
    func setVisitorView() {
        
        addSubview(imageView)
        addSubview(houseImage)
        addSubview(maskImage)
        addSubview(titleLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        //取消autoresizing，使用纯代码默认是autoresizing
        for subView in subviews {
            subView.translatesAutoresizingMaskIntoConstraints = false
        }
        
        //添加约束
        addConstraint(NSLayoutConstraint(item: imageView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: imageView,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: -50))
        
        //houseImage
        addConstraint(NSLayoutConstraint(item: houseImage,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: imageView,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: houseImage,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: imageView,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: 0))
        
        //maskImage
        addConstraint(NSLayoutConstraint(item: maskImage,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: imageView,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: maskImage,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: imageView,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: 0))
        
        //titleLabel
        addConstraint(NSLayoutConstraint(item: titleLabel,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: imageView,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 20))
        
        addConstraint(NSLayoutConstraint(item: titleLabel,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: titleLabel,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 236))
        
        //registerBtn
        addConstraint(NSLayoutConstraint(item: registerBtn,
                                         attribute: .left,
                                         relatedBy: .equal,
                                         toItem: titleLabel,
                                         attribute: .left,
                                         multiplier: 1.0,
                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: registerBtn,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: titleLabel,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 10))
        
        addConstraint(NSLayoutConstraint(item: registerBtn,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 0,
                                         constant: 80))
        
        //loginBtn
        addConstraint(NSLayoutConstraint(item: loginBtn,
                                         attribute: .right,
                                         relatedBy: .equal,
                                         toItem: titleLabel,
                                         attribute: .right,
                                         multiplier: 1.0,
                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginBtn,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: titleLabel,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 10))
        
        addConstraint(NSLayoutConstraint(item: loginBtn,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 0,
                                         constant: 80))
    }
    
    
       @objc func startAnimation() {
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.repeatCount = MAXFLOAT
        //动画持续时间
        animation.duration = 15
        animation.toValue = Double.pi * 2
        
        animation.isRemovedOnCompletion = false
        
        imageView.layer.add(animation, forKey: "transform.rotation")
    }
}

